本文讲解如何在pc网页上完成支付宝的支付功能, 详细讲解了支付宝的配置信息,项目如何使用配置信息等, 本项目中代码可以直接运行, 也可以将代码迁移至你自己的项目中直接使用。 注意: 下面讲解的功能只能用在pc网页上进行支付, 不能用于其它场景支付。 运行环境
Eclipse JDK1.6及以上 Tomcat6.0及以上
使用方法
SDK下载地址:
https://doc.open.alipay.com/docs/doc.htm?treeId=193&articleId=103419&docType=1
第一步;请下载【JAVA版资源】的SDK。
第二步:下载完毕后,把压缩包解压。
第三步:压缩包alipay-sdk-javaXXX.jar、commons-logging-1.1.1.jar架包文件放入工程项目alipay.trade.page.pay-PHP-UTF-8\WebContent\WEB-INF\lib中。
第四步:打开AlipayConfig.java文件,设置请求参数信息并保存,请求参数信息有:app_id、merchant_private_key、alipay_public_key、return_url、notify_url。
bizContent的各个请求参数用法与拼接方式,见文档:https://doc.open.alipay.com/docs/doc.htm?treeId=270&articleId=105899&docType=1
第五步:运行index.jsp文件
第六步:体验支付流程,看效果等。
业务处理注意事项
请配置notify_url文件、return_url文件,其中,notify_url文件主要是写入业务处理逻辑代码,请结合自身情况谨慎编写。
如何验证异步通知数据?
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号
2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额)
3、校验通知中的seller_id(或者seller_email) 是否为该笔交易对应的操作方(一个商户可能有多个seller_id/seller_email)
4、验证接口调用方的app_id
说明
本demo仅仅为学习参考使用,请根据实际情况自行开发,把功能嵌入您的项目或平台中。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>付款</title></head><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page import="com.alipay.config.*"%><%@ page import="com.alipay.api.*"%><%@ page import="com.alipay.api.request.*"%><%-- <%@ page import="com.alipay.sdk.app.EnvUtils"%> --%><% //获得初始化的AlipayClient //com.alipay.sdk.app.EnvUtils /* EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX); */ AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(AlipayConfig.return_url); alipayRequest.setNotifyUrl(AlipayConfig.notify_url); //商户订单号,商户网站订单系统中唯一订单号,必填 String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //付款金额,必填 String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8"); //订单名称,必填 String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8"); //商品描述,可空 String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8"); alipayRequest.setBizContent("{\"out_trade_no\":\"" out_trade_no "\"," "\"total_amount\":\"" total_amount "\"," "\"subject\":\"" subject "\"," "\"body\":\"" body "\"," "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明 //alipayRequest.setBizContent("{\"out_trade_no\":\"" out_trade_no "\"," // "\"total_amount\":\"" total_amount "\"," // "\"subject\":\"" subject "\"," // "\"body\":\"" body "\"," // "\"timeout_express\":\"10m\"," // "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节 //请求 String result = alipayClient.pageExecute(alipayRequest).getBody(); System.out.println("result:" result); //输出 out.println(result);%><body></body></html>
4.2 交易查询
123456789101112131415161718192021222324252627282930313233343536<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>交易查询</title></head><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page import="com.alipay.config.*"%><%@ page import="com.alipay.api.*"%><%@ page import="com.alipay.api.request.*"%><% //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest(); //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = new String(request.getParameter("WIDTQout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("WIDTQtrade_no").getBytes("ISO-8859-1"),"UTF-8"); //请二选一设置 alipayRequest.setBizContent("{\"out_trade_no\":\"" out_trade_no "\"," "\"trade_no\":\"" trade_no "\"}"); //请求 String result = alipayClient.execute(alipayRequest).getBody(); //输出 out.println(result);%><body></body></html>
4.3 交易退款
12345678910111213141516171819202122232425262728293031323334353637383940414243444546<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>退款</title></head><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page import="com.alipay.config.*"%><%@ page import="com.alipay.api.*"%><%@ page import="com.alipay.api.request.*"%><% //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest(); //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = new String(request.getParameter("WIDTRout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("WIDTRtrade_no").getBytes("ISO-8859-1"),"UTF-8"); //请二选一设置 //需要退款的金额,该金额不能大于订单金额,必填 String refund_amount = new String(request.getParameter("WIDTRrefund_amount").getBytes("ISO-8859-1"),"UTF-8"); //退款的原因说明 String refund_reason = new String(request.getParameter("WIDTRrefund_reason").getBytes("ISO-8859-1"),"UTF-8"); //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传 String out_request_no = new String(request.getParameter("WIDTRout_request_no").getBytes("ISO-8859-1"),"UTF-8"); alipayRequest.setBizContent("{\"out_trade_no\":\"" out_trade_no "\"," "\"trade_no\":\"" trade_no "\"," "\"refund_amount\":\"" refund_amount "\"," "\"refund_reason\":\"" refund_reason "\"," "\"out_request_no\":\"" out_request_no "\"}"); //请求 String result = alipayClient.execute(alipayRequest).getBody(); //输出 out.println(result);%><body></body></html>
评论